#pragma once
#include<iostream>
#include<sys/types.h> //mkfifo
#include<sys/stat.h> //mkfifo
#include<unistd.h> //unlink
#include<fcntl.h>

#define PIPE_PATH "./pipe!!!"

#define ERR_INFO(input) \
    do                  \
    {                   \
        perror(input);  \
        exit(1);        \
    } while (0);        \

class NamedPipe
{
public:
    NamedPipe()
    {
        
    }
    void Create()
    {
        _fd = mkfifo(PIPE_PATH,0666);
        if(_fd < 0)
        {
            ERR_INFO("mkfifo");
        }
    }
    void Awake()
    {
        _fd = open(PIPE_PATH,O_WRONLY | O_APPEND);
        char ch = 'c';
        write(_fd,&ch,1);
    }
    bool Wait()
    {
        _fd = open(PIPE_PATH,O_RDONLY);
        char ch = ' ';
        read(_fd,&ch,1);
        if(ch == 'c')
            return true;
        else
            return false;
    }
    void Destory()
    {
        unlink(PIPE_PATH);
        close(_fd);
    }
    ~NamedPipe()
    {
 
    }
private:
    int _fd;
};

